
#include <stdbool.h>
#include "bsp_usart.h"
#include "string.h"
#include "bluetooth.h"

/**
 * @brief 串口1中断服务函数
 *
 * @retval 无
 * */
void DEBUG_USART1_IRQHandler() {
	if (USART_GetITStatus(DEBUG_USART1, USART_IT_RXNE) != RESET)
	{
		USART_SendData(DEBUG_USART1, USART_ReceiveData(DEBUG_USART1));
	}
}

/**
 * @brief 串口2中断服务函数
 *
 * @retval 无
 * */
void DEBUG_USART2_IRQHandler() {
	Usart2_SendArray("A", 1);
	if (USART_GetITStatus(DEBUG_USART2, USART_IT_RXNE) != RESET)
	{

		//printf("%c", Usart2_ReceiveByte());
		//Usart2_SendByte(Usart2_ReceiveByte());

		//USART_ClearITPendingBit(DEBUG_USART2, USART_FLAG_RXNE);
		USART_SendData(DEBUG_USART2, USART_ReceiveData(DEBUG_USART2));
	}
}

/**
 * @brief 蓝牙接收数据接口
 *
 * @retval 接收到的一字节数据
 * */
static uint8_t RecDataInterface()
{
    return USART_ReceiveData(USART3); // 接收数据
}

/**
 * @brief 串口3中断服务函数
 *
 * @retval 无
 * */
void USART3_IRQHandler(void) {
    if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) {
        //蓝牙接收处理数据
        BluetoothRectData(RecDataInterface);
    }
}


/**
 * @brief 串口4中断服务函数
 *
 * @retval 无
 * */
void UART4_IRQHandler() {
	if (USART_GetITStatus(DEBUG_USART4, USART_IT_RXNE) != RESET)
	{
		USART_SendData(DEBUG_USART4, USART_ReceiveData(DEBUG_USART4));
	}
}

/**
 * @brief 串口5中断服务函数
 *
 * @retval 无
 * */
void DEBUG_USART5_IRQHandler() {
	if (USART_GetITStatus(DEBUG_USART5, USART_IT_RXNE) != RESET)
	{
		USART_SendData(DEBUG_USART5, USART_ReceiveData(DEBUG_USART5));
	}
}

/**
 * @brief 串口1 中断初始化
 *
 * @param pre	抢占优先级
 * @param sub	子优先级
 *
 * @retval 无
 * */
void Usart1_NVIC_Init(uint8_t pre, uint8_t sub) {
	NVIC_InitTypeDef NVIC_InitStructure;

	/* 使能中断 */
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	/* 配置USART为中断源 */
	NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART1_IRQ;
	/* 抢断优先级*/
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre;
	/* 子优先级 */
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub;
	/* 初始化配置NVIC */
	NVIC_Init(&NVIC_InitStructure);

	//使能接收中断
	USART_ITConfig(DEBUG_USART1, USART_IT_RXNE, ENABLE);
}

/**
 * @brief 中断服务函数
 *
 * @param pre 抢占优先级
 * @param sub 子优先级
 *
 * @retval 无
 * */
void Usart2_NVIC_Init(uint8_t pre, uint8_t sub) {
	NVIC_InitTypeDef NVIC_InitStructure;

	/* 使能中断 */
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	/* 配置USART为中断源 */
	NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART2_IRQ;
	/* 抢断优先级*/
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre;
	/* 子优先级 */
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub;
	/* 初始化配置NVIC */
	NVIC_Init(&NVIC_InitStructure);

	//使能接收中断
	USART_ITConfig(DEBUG_USART2, USART_IT_RXNE, ENABLE);
}

/**
 * @brief 串口3 中断服务函数
 *
 * @param pre 抢占优先级
 * @param sub 子优先级
 *
 * @retval 无
 * */
void Usart3_NVIC_Init(uint8_t pre, uint8_t sub) {
	NVIC_InitTypeDef NVIC_InitStructure;

	/* 使能中断 */
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	/* 配置USART为中断源 */
	NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART3_IRQ;
	/* 抢断优先级*/
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre;
	/* 子优先级 */
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub;
	/* 初始化配置NVIC */
	NVIC_Init(&NVIC_InitStructure);

	//使能接收中断
	USART_ITConfig(DEBUG_USART3, USART_IT_RXNE, ENABLE);
}

#if 0

/**
 * @brief 串口4 中断服务函数
 *
 * @param pre 抢占优先级
 * @param sub 子优先级
 *
 * @retval 无
 * */
void Usart4_NVIC_Init(uint8_t pre, uint8_t sub) {
	NVIC_InitTypeDef NVIC_InitStructure;

	/* 使能中断 */
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	/* 配置USART为中断源 */
	NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART4_IRQ;
	/* 抢断优先级*/
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre;
	/* 子优先级 */
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub;
	/* 初始化配置NVIC */
	NVIC_Init(&NVIC_InitStructure);

	//使能接收中断
	USART_ITConfig(DEBUG_USART4, USART_IT_RXNE, ENABLE);
}

/**
 * @brief 串口5 中断服务函数
 *
 * @param pre 抢占优先级
 * @param sub 子优先级
 *
 * @retval 无
 * */
void Usart5_NVIC_Init(uint8_t pre, uint8_t sub) {
	NVIC_InitTypeDef NVIC_InitStructure;

	/* 使能中断 */
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	/* 配置USART为中断源 */
	NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART5_IRQ;
	/* 抢断优先级*/
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre;
	/* 子优先级 */
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub;
	/* 初始化配置NVIC */
	NVIC_Init(&NVIC_InitStructure);

	//使能接收中断
	USART_ITConfig(DEBUG_USART5, USART_IT_RXNE, ENABLE);
}

#endif


